import storage from 'store'
import {RELEASE_TYPE, VERSION} from "@/store/mutation-types";
import {getVersion} from "@/api/system";

const app = {
    state: {
        version: '',    // 版本
        releaseType: '' // 发布类型
    },
    mutations: {
        [VERSION]: (state, version) => {
            state.version = version
            storage.set(VERSION, version)
        },
        [RELEASE_TYPE]: (state, releaseType) => {
            state.releaseType = releaseType
            storage.set(RELEASE_TYPE, releaseType)
        }
    },
    actions: {
        GetVersion ({ commit } ) {
            return new Promise( ((resolve, reject) => {
                getVersion().then(res => {
                    res = res.data
                    if (res.success) {
                        const {version, releaseType} = res.data
                        commit(VERSION, version)
                        commit(RELEASE_TYPE, releaseType)
                    }
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            }))
        }
    }
}

export default app
